/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;
import java.util.Calendar;
import javax.servlet.*;
import javax.servlet.http.*;



/**
 *
 * @author aa
 */
public class fiboServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    

   public String infoWaktu(GregorianCalendar cal) {
      String hari[] = {"Dummy", "Minggu", "Senin",
                       "Selasa", "Rabu", "Kamis",
                       "Jumat", "Sabtu"};
      String bulan[] = {
         "Januari", "Februari", "Maret",
         "April", "Mei", "Juni", "Juli",
         "Agustus", "September", "Oktober",
         "November", "Desember"};
      return(
          hari[cal.get(Calendar.DAY_OF_WEEK)] +
          ", " +
          cal.get(Calendar.DATE) + " " +
          bulan[cal.get(Calendar.MONTH)] + " " +
          cal.get(Calendar.YEAR));
   }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

      out.write('\n');
      out.write('\n');
      out.write('\n');
      out.write("\n");
      out.write("<html>\n");
      out.write("<head>\n");
      out.write("<title>Untitled Document</title>\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
      out.write("</head>\n");
      out.write("\n");
      out.write("<body onLoad=\"start time()\" bgcolor=\"#99CCFF\">\n");
      out.write("<table width=\"100%\" border=\"0\">\n");
      out.write("  \n");
      out.write("  <tr>\n");
      out.write("    <td colspan=\"2\"><font color=\"#0000CC\" size=\"3\">\n");
      out.write("      ");

   int pencacah = 0;
   String strPencacah = "";

   Cookie[] dafCookie = request.getCookies();
   if (dafCookie != null) {
      Cookie cookie;

      for (int i=0; i < dafCookie.length; i++) {
         cookie = dafCookie[i];
         if (cookie.getName().equals("pencacah")) {
            strPencacah = cookie.getValue();
            pencacah = Integer.valueOf(strPencacah).intValue();
            break;
         }
      }
   }

   pencacah++;
   Integer bil = new Integer(pencacah);

   Cookie cookie = new Cookie("pencacah", bil.toString());
   cookie.setMaxAge(360 * 24 * 60 * 60);
   response.addCookie(cookie);

   out.println("Anda telah mengunjungi halaman ini " + pencacah + " kali");


      out.write("\n");
      out.write("      </font> </td>\n");
      out.write("    <td width=\"23%\">\n");
      out.write("      \n");
      out.write("      ");
      out.write("\n");
      out.write("      ");

   GregorianCalendar sekarang =
      new GregorianCalendar();
   out.println(infoWaktu(sekarang) + "<BR>");

      out.write("\n");
      out.write("    </td>\n");
      out.write("  </tr>\n");
      out.write("  <tr>\n");
      out.write("    <td width=\"26%\"><div align=\"center\"> </div></td>\n");
      out.write("    <td colspan=\"2\"><div align=\"right\">waktu sekarang : ");
      out.print( new java.util.Date() );
      out.write("\n");
      out.write("      </div></td>\n");
      out.write("  </tr>\n");
      out.write("  <form action=\"bintang\" method=\"post\" name=\"bintang\" id=\"bintang\">\n");
      out.write("  <p align=\"center\"><strong><font color=\"#CC0066\" size=\"5\">Silakan Isi Form Di\n");
      out.write("    Bawah ini</font></strong></p>\n");
      out.write("  <table>\n");
      out.write("  <tr>\n");
      out.write("      <td height=\"29\"><font color=\"#FFFFFF\"><strong>Besar Angka</strong></font></td>\n");
      out.write("      <td><font color=\"#999999\"><strong>:</strong></font></td>\n");
      out.write("      <td colspan=\"2\"><font color=\"#000000\">\n");
      out.write("        <input name=\"angka\" type=\"text\" value=\"\" size=\"35\" maxlength=\"35\">\n");
      out.write("        </font></td>\n");
      out.write("  </tr>\n");
      out.write("  </table>\n");
      out.write("  <table>\n");
      out.write("  <tr align=\"center\"><font color=\"#FFFF00\">tekan submit jika sudah yakin !!!</font></tr>\n");
      out.write("  <tr align=\"center\">\n");
      out.write("    <input type=\"submit\" name=\"Submit\" value=\"Kirim\">\n");
      out.write("    <input type=\"reset\" name=\"Submit2\" value=\"Batal\">\n");
      out.write("  </tr>\n");
      out.write("</table>\n");
      out.write("</form></table>\n");
      out.write("<p>&nbsp;</p></body>\n");
      out.write("</html>\n");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
